/**
 * 
 */
package habitcoach.core.reminder;

import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Coddy created on 2010-4-25
 * 
 */
public class DateChangeEventPublisherTest
{
	DateChangeEventPublisherImpl publisher;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{
		publisher = new DateChangeEventPublisherImpl();
	}

	/**
	 * Test method for
	 * {@link habitcoach.core.reminder.DateChangeEventPublisher#update()}.
	 */
	@Test
	public void testUpdate()
	{
		Listener listener = new Listener();
		publisher.addListerner(listener);
		publisher.update();
		Assert.assertEquals(10, listener.data);
	}

	/**
	 * Test method for
	 * {@link habitcoach.core.reminder.DateChangeEventPublisher#addListerner(habitcoach.core.reminder.DateChangeEventListener)}
	 * .
	 */
	@Test
	public void testAddListerner()
	{
		List list = publisher.getListeners();
		Assert.assertEquals(0, list.size());
		publisher.addListerner(new Listener());
		Assert.assertEquals(1, list.size());
		publisher.addListerner(new Listener());
		publisher.addListerner(new Listener());
		publisher.addListerner(new Listener());

		Assert.assertEquals(4, list.size());
	}

	/**
	 * Test method for
	 * {@link habitcoach.core.reminder.DateChangeEventPublisher#removeListener(habitcoach.core.reminder.DateChangeEventListener)}
	 * .
	 */
	@Test
	public void testRemoveListener()
	{
		Assert.assertEquals(0, publisher.getListeners().size());
		Listener l1 = new Listener();
		publisher.addListerner(l1);
		Assert.assertEquals(1, publisher.getListeners().size());
		Listener l2 = new Listener();
		publisher.addListerner(l2);
		Assert.assertEquals(2, publisher.getListeners().size());

		publisher.removeListener(l1);
		Assert.assertEquals(1, publisher.getListeners().size());
		Assert.assertTrue(publisher.getListeners().contains(l2));
		Assert.assertFalse(publisher.getListeners().contains(l1));
	}

	@Test
	public void testRemoveListenerInvalid()
	{
		Listener listener = new Listener();
		publisher.removeListener(listener);
		Assert.assertEquals(0, publisher.getListeners().size());

		Listener l1 = new Listener();
		publisher.addListerner(l1);
		Assert.assertEquals(1, publisher.getListeners().size());

		publisher.removeListener(l1);
		Assert.assertEquals(0, publisher.getListeners().size());
		publisher.removeListener(l1);
		Assert.assertEquals(0, publisher.getListeners().size());

	}

	class Listener implements DateChangeEventListener
	{
		int data;

		/*
		 * (non-Javadoc)
		 * 
		 * @see habitcoach.core.reminder.DateChangeEventListener#onDateChanged()
		 */
		public Listener()
		{
			super();
			data = 0;
		}

		@Override
		public void onDateChanged()
		{
			// TODO Auto-generated method stub
			data = 10;
		}
	}
}
